package com.woniuxy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.exception.customException.TheAddedHasBeenExist;
import com.woniuxy.system.entity.AssessmentItems;
import com.woniuxy.system.entity.DataRecord;
import com.woniuxy.system.entity.ItemIndi;
import com.woniuxy.system.entity.PerformanceUser;
import com.woniuxy.system.mapper.AssessmentItemsMapper;
import com.woniuxy.system.mapper.DataRecordMapper;
import com.woniuxy.system.mapper.ItemIndiMapper;
import com.woniuxy.system.service.AssessmentItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 考核项 服务实现类
 * </p>
 *
 * @author woniuxy
 * @since 2022-06-01
 */
@Service
@Transactional
public class AssessmentItemsServiceImpl extends ServiceImpl<AssessmentItemsMapper, AssessmentItems> implements AssessmentItemsService {

    @Autowired
    AssessmentItemsMapper assessmentItemsMapper;
    @Autowired
    DataRecordMapper dataRecordMapper;
    @Autowired
    ItemIndiMapper itemIndiMapper;



    //添加考核指标
    @Override
    public boolean addAssessmentItem(AssessmentItems assessmentItems, String indicatorId, String userid) throws TheAddedHasBeenExist {
        LambdaQueryWrapper<AssessmentItems> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AssessmentItems::getName,assessmentItems.getName());
        List<AssessmentItems> list = assessmentItemsMapper.selectList(wrapper);
        if(list.size()>0){
            throw new TheAddedHasBeenExist();
        }
        DataRecord dataRecord = new DataRecord();
        dataRecord.setCreateTime(new Date());
        dataRecord.setUserId(Long.valueOf(userid));
        dataRecord.setInfo("新建考核项");
        dataRecordMapper.insert(dataRecord);
        assessmentItems.setRecordId(dataRecord.getId());
        int insert = assessmentItemsMapper.insert(assessmentItems);
        if(insert==1){
            ItemIndi itemIndi = new ItemIndi();
            itemIndi.setIndiId(Long.valueOf(indicatorId));
            itemIndi.setItemId(assessmentItems.getId());
            int insert1 = itemIndiMapper.insert(itemIndi);
            if(insert1==1){
                return true;
            }
        }
        return false;
    }

    @Override
    public Page<AssessmentItems> getAllAssessmentItems(Page<AssessmentItems> page, QueryWrapper<AssessmentItems> wrapper) {
        return assessmentItemsMapper.getAllAssessmentItems(page,wrapper);
    }


}
